﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MadLibrary.Web
{
    /// <summary>
    /// Holds information about directory primary intended for web uses.
    /// </summary>
    public class WebDirectory : WebFile
    {
        /// <summary>
        /// Gets or sets WebFile collection of current web directory.
        /// </summary>
        public WebFiles WebFiles { get; set; }
        /// <summary>
        /// Creates new empty web directory.
        /// </summary>
        public WebDirectory() : base()
        {
            this.WebFiles = new WebFiles();
        }
        /// <summary>
        /// Creates new web directory with specified name.
        /// </summary>
        /// <param name="Name">Specifies name of new web directory.</param>
        public WebDirectory(string Name)
            : base(Name)
        {
            this.WebFiles = new WebFiles();
        }
        /// <summary>
        /// Creates new web directory with specified web files.
        /// </summary>
        /// <param name="WebFiles">Specifies web files which have to be defaultly added to new web directory.</param>
        public WebDirectory(params WebFile[] WebFiles)
            : base()
        {
            this.WebFiles = new WebFiles(WebFiles);
        }
        /// <summary>
        /// Creates new web directory with specified name and specified web files.
        /// </summary>
        /// <param name="Name">Specifies name of new web directory.</param>
        /// <param name="WebFiles">Specifies web files which have to be defaultly added to new web directory.</param>
        public WebDirectory(string Name, params WebFile[] WebFiles)
            : base(Name)
        {
            this.WebFiles = new WebFiles(WebFiles);
        }
        /// <summary>
        /// Loads web files from specified path to current web directory.
        /// </summary>
        /// <param name="Path">Specifies path of directory with files which have to be loaded to current web directory.</param>
        public override void LoadContent(string Path)
        {
            string[] paths = System.IO.Directory.GetFiles(Path);
            foreach(string path in paths)
            {
                WebFile w = new WebFile();
                if (path.EndsWith(".html") || path.EndsWith(".htm"))
                    w = new HtmlDocument(path);
                else if (path.EndsWith(".css"))
                {
                    w = new CssDocument();
                    w.LoadContent(path);
                }
                else if (!path.Split('\\')[path.Split('\\').Length - 1].Contains("."))
                {
                    w = new WebDirectory();
                    w.LoadContent(path);
                }
                else w.LoadContent(path);
            }
        }
        /// <summary>
        /// Selects HtmlDocument from specified HTTP call path.
        /// </summary>
        /// <param name="HttpCallPath">Specifies path of file which has be returned.</param>
        /// <returns>HtmlDocument on specified path.</returns>
        public HtmlDocument GetPage(string HttpCallPath)
        {
            string curr = "";
            if (HttpCallPath.Contains('/'))
                curr = HttpCallPath.Split('/')[0];
            else curr = HttpCallPath;
            if (curr.Contains("?"))
                curr = curr.Split('?')[0];
            if (curr.EndsWith(".html") || curr.EndsWith(".htm"))
                return this.WebFiles[curr] as HtmlDocument;
            else
            {
                return (this.WebFiles[curr] as WebDirectory).GetPage(HttpCallPath.Remove(0, HttpCallPath.IndexOf('/')));
            }
        }
        /// <summary>
        /// Selects CssDocument from specified path.
        /// </summary>
        /// <param name="HttpCallPath">Specifies path of file which has to be loaded.</param>
        /// <returns>File on specified path.</returns>
        public CssDocument GetStyles(string HttpCallPath)
        {
            string curr = "";
            if (HttpCallPath.Contains('/'))
                curr = HttpCallPath.Split('/')[0];
            else curr = HttpCallPath;
            if (curr.Contains("?"))
                curr = curr.Split('?')[0];
            if (curr.EndsWith(".css"))
                return this.WebFiles[curr.Split('.')[0]] as CssDocument;
            else
            {
                return (this.WebFiles[curr] as WebDirectory).GetStyles(HttpCallPath.Remove(0, HttpCallPath.IndexOf('/')));
            }
        }
    }
}
